package com.nextel.project.agps;

import com.motorola.iden.position.AggregatePosition;
import com.motorola.iden.position.PositionConnection;
import java.io.IOException;
import javax.microedition.io.Connector;
import com.nextel.project.agps.PropertiesMovil;

/**
 *
 * @author marizaga
 */
public class GPSThread implements Runnable {
    
    private PositionConnection posCon;
    private Thread thread = null;
    private int fixDelay;
//    private boolean firstFix = true;
    private String positionString;
//    private boolean fixExtended;
//    private boolean stopped = false;
//    private byte currentStatus;
    
    private AGPSBean bean;
    
    public GPSThread(AGPSBean bean, boolean fixExtended, int fixDelay) {
        //  this.positionString = bean.getMposition();
//        this.fixExtended = fixExtended;
        this.fixDelay = fixDelay;
        this.bean = bean;
    }
    
    public void start() {
        thread = new Thread(this);
        thread.start();
    }
    
    
    public void run() {
            posCon = null;
            AggregatePosition pos = null;

            this.positionString = AGPSBean.DELAY_NO;

            while (true) {

                try {

                    posCon = (PositionConnection) Connector.open("mposition:" +  positionString);

                   // validateFix(posCon);

                    pos = posCon.getPosition();
                    // System.out.println("captura por celda");
                    bean.printPosition(pos);

                    poolFix(posCon, pos);

                } catch (Exception ex) {
                    
                    try{ Thread.sleep(2000);} catch ( Exception e ){ }
                    
                    if (posCon != null &&
                        ( posCon.getStatus() == PositionConnection.POSITION_RESPONSE_ERROR ||
                        posCon.getStatus() == PositionConnection.POSITION_NO_RESPONSE)) {
                    this.positionString = AGPSBean.DELAY_NO;
                    }
                }finally{
                    cerrarConexion();


                }
            }
    }
    
    private void validateFix(PositionConnection posCon) throws Exception {
        
        PropertiesMovil.getMovilActualiza( );
        
        PropertiesMovil.errorConexionGPS = 0;
        
        switch(posCon.getStatus()) {
            case -1: PropertiesMovil.errorConexionGPS = posCon.getStatus();
            break;
            
            case -2: PropertiesMovil.errorConexionGPS =posCon.getStatus();
            throw new Exception("NoResponseException");//NoResponseException
            
            case -3: PropertiesMovil.errorConexionGPS =posCon.getStatus();
            throw new Exception("ResponseErrorException");//ResponseErrorException
            
            case -5:
            case -4: PropertiesMovil.errorConexionGPS =posCon.getStatus();
            throw new Exception("PositionException");//PositionException
            case 0: case 1: default: break;
        }
    }
    
    private void poolFix(PositionConnection posCon, AggregatePosition pos)
            throws Exception  {

        while (true) {
            try {

                Thread.sleep(fixDelay);

                if (posCon != null &&
                        ( posCon.getStatus() == PositionConnection.POSITION_RESPONSE_ERROR ||
                        posCon.getStatus() == PositionConnection.POSITION_NO_RESPONSE)) {
                   // System.out.println("poolFix.Connector.open>DELAY_LOW");
                    this.positionString = AGPSBean.DELAY_LOW;

                posCon = (PositionConnection) Connector.open("mposition:" + positionString);
                validateFix(posCon);
                pos = posCon.getPosition();

                } else  {
                   // System.out.println("poolFix.getPosition>" +positionString);
                   pos = posCon.getPosition(positionString);
                   if(!AGPSBean.DELAY_NO.equals(positionString)){

                     validateFix(posCon);
                   }
                }

                bean.printPosition(pos);

                positionString = AGPSBean.DELAY_LOW;


            } catch (Exception ex) {

                if (posCon != null &&
                        ( posCon.getStatus() == PositionConnection.POSITION_RESPONSE_ERROR ||
                        posCon.getStatus() == PositionConnection.POSITION_NO_RESPONSE)) {
                    this.positionString = AGPSBean.DELAY_NO;
                    cerrarConexion( );
                   // System.out.println("Error."+ex.getMessage( )+"Connector.open>DELAY_NO");
                    posCon = (PositionConnection) Connector.open("mposition:" +  positionString );
                    pos = posCon.getPosition(positionString);
                    bean.printPosition(pos);
                    positionString = AGPSBean.DELAY_LOW + ";" + AGPSBean.FIX_EXTENDED ;
                }
            }
        }
    }
    
    
    public void cerrarConexion( ){
       // try{
       //    if(posCon != null){
        ///    //    posCon.close( );
        //    }
       // }catch(Exception e){System.out.println("error cerrando conexion" + e.getMessage( ));}
    }
    
//    public boolean isFirstFix() {
//        return firstFix;
//    }
    
//    public byte getCurrentStatus() {
//        return currentStatus;
//    }
}
